import { isWeiXinLogin,getQueryString } from '@/utils/str.js'

// import { Userlogin } from '@/commApi/dingcengApi/index.js'
// import { getUserInfo } from '@/commApi/dingcengApi/index.js'
import {getUserInfo,} from "@/pages/indoor/api/home/index.js"
import {Userlogin,} from "@/pages/indoor/api/personal/index.js"
let shuaxins = false
const state = {
   userInfo:uni.getStorageSync('userInfo'),
}
let getTokens = false
const mutations = {
	SET_USERINFO_TOKEN: (state, userInfo) => {
		// state.userInfo = userInfo
		// uni.setStorageSync('userInfo', userInfo)
	},
}
const actions = {
	async setTokenUserInfo({
		commit,
		state
	}, payload) {
		let token = uni.getStorageSync('token')
		console.log('token',token)
		// 获取服务供应商
		let Provider = await uni.getProvider({service: 'oauth'})
		console.log('Provider',Provider)
		// #ifdef MP-ALIPAY
		console.log('authCode',999)
		let authCode = await my.getAuthCode({scopes: 'auth_base'});
		console.log('authCode',authCode)
		// return
		// #endif
		var data = {}
		// if(getTokens) return
		// getTokens = true
		if(token){
			let res = ''
			res = await getUserInfo()
			//获取token的flag
			huanyuanFlag(payload)
			if(!res) return 
			data = res.data
			uni.setStorageSync('userInfo',data)
			return true
		}else{
			var res = {}
			// #ifdef MP-ALIPAY
			console.log('userLogin')
			res = await Userlogin({
				appId:'2021004147647460',
				code:authCode.authCode,
			})
			data = res.data || {}
			console.log('登录',res)
			// #endif
			await uni.setStorageSync('token', data.token)
			// huanyuanFlag(payload)
			let userIndos = await getUserInfo()
			if(!userIndos) return
			uni.setStorageSync('userInfo',userIndos.data)
			shuaxin(payload.that)
		}
		commit('SET_USERINFO_TOKEN', data);
		return true
	},
}

// 还原初始值
function huanyuanFlag(payload){
	shuaxins = false
	getTokens = false
}

async function shuaxin(payload){
	// if(shuaxins) return
	// shuaxins = true
	// 页面重载
	const pages = getCurrentPages()
	// 声明一个pages使用getCurrentPages方法
	const curPage = pages[pages.length - 1]
	// 声明一个当前页面
	// #ifndef H5
	curPage.$vm.fetchSomething(curPage.options)
	// curPage.onShow()
	// await curPage.onLoad(curPage.options) // 传入参数
	// curPage.onReady()
	
	// #endif
	
	// 执行刷新
}


export default {
	namespaced: true,
	state,
	mutations,
	actions,
}

